﻿using System;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using ClientOverflow.DataObjects;

namespace ClientOverflow.CommunicationServices.Data
{
    /// <summary>
    /// DataAccessInitializer class is in charge of initializing the data access to DB using ActiceRecord.
    /// The DataAccessInitializer will make sure that all types in ClientOverflow.DataObjects have matching
    /// tables in the database.
    /// </summary>
    public static class DataAccessInitializer
    {
        private static bool isInitialized;

        public static void Initialize()
        {
            if (isInitialized)
            {
                return;
            }

            isInitialized = true;
            var buildForMsSqlServer = InPlaceConfigurationSource.BuildForMSSqlServer(@".\SQLEXPRESS", "ClientOverflow");
            buildForMsSqlServer.PluralizeTableNames = true;
            try
            {
                ActiveRecordStarter.Initialize(typeof(Answer).Assembly, buildForMsSqlServer);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            ActiveRecordStarter.UpdateSchema();
        }
    }
}